﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PBOffice.Models;
using System.Data;
using System.Data.SqlClient;
namespace PBOffice.DAL
{
    public class LoginLogService
    {
        public List<LoginLog> GetAllLoginLog()
        {
            List<LoginLog> list = new List<LoginLog>();
            string sql = "select * from LoginLog";
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    LoginLog log = new LoginLog();
                    log.LoginId = (int)dr["LoginId"];
                    log.UserId = dr["UserId"].ToString();
                    log.LoginTime = Convert.ToDateTime(dr["LoginTime"]);
                    log.IfSuccess = (int)dr["IfSuccess"];
                    log.LoginUserIp = dr["LoginUserIp"].ToString();
                    log.LoginDesc = dr["LoginDesc"].ToString();
                    list.Add(log);
                }
            }
            return list;
        }
        public List<LoginLog> GetAllLoginLog(DateTime begin, DateTime end)
        {
            List<LoginLog> list = new List<LoginLog>();
            string sql = string.Format("select * from LoginLog where LoginTime Between '{0}' and '{1}'", begin, end);
            using (IDataReader dr = SqlHelp.ExecuteReader(SqlHelp.connectionstring, CommandType.Text, sql))
            {
                while (dr.Read())
                {
                    LoginLog log = new LoginLog();
                    log.LoginId = (int)dr["LoginId"];
                    log.UserId = dr["UserId"].ToString();
                    log.LoginTime = Convert.ToDateTime(dr["LoginTime"]);
                    log.IfSuccess = (int)dr["IfSuccess"];
                    log.LoginUserIp = dr["LoginUserIp"].ToString();
                    log.LoginDesc = dr["LoginDesc"].ToString();
                    list.Add(log);
                }
            }
            return list;
        }
        public void DeleteLoginLog(int id)
        {
            string sql = string.Format("delete [LoginLog] where LoginId={0}", id);
            SqlHelp.ExecuteNonQuery(SqlHelp.connectionstring, CommandType.Text, sql);
        }
    }
}
